//
//  HRAllMingJiaViewController.m
//  Economical report
//
//  Created by wyzc on 16/6/28.
//  Copyright © 2016年 叶落知秋. All rights reserved.
//
#define CELLID @"CELL"

#import "HRAllMingJiaViewController.h"
#import "AFNetworking.h"
#import "HRAllMingJiaModel.h"
#import "HRAllMingJiaCell.h"
#import "HRJinRongViewController.h"
#import "HRAllMingJiaCell.h"

@interface HRAllMingJiaViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataSourceArr;

@end

@implementation HRAllMingJiaViewController
-(NSMutableArray *)dataSourceArr
{
    if (_dataSourceArr==nil) {
        _dataSourceArr=[NSMutableArray array];
    }
    return _dataSourceArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setButton];
    [self createXib];
    [self sendRequestData];
}
//添加back按钮
-(void)setButton

{
    
}
//注册xib
-(void)createXib
{
    UINib *nib=[UINib nibWithNibName:@"HRAllMingJiaCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:CELLID];
    self.view.backgroundColor=HRCOLOR(239, 239, 239);
    self.collectionView.backgroundColor=[UIColor clearColor];
    
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize=CGSizeMake(UPSreenW*0.2, UPSreenH*0.2);
    self.collectionView.contentInset=UIEdgeInsetsMake(50 , 50, 50, 50);
    flowLayout.minimumLineSpacing=0;
    flowLayout.minimumInteritemSpacing=0;
    self.collectionView.collectionViewLayout=flowLayout;
}
//发送网络请求
-(void)sendRequestData
{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:@"http://apis.21jingji.com/author/getList?page=1" parameters:@"abc" progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *dataDict) {
        NSArray *arr=dataDict[@"list"];
        for (NSDictionary *dict in arr) {
            HRAllMingJiaModel *model=[HRAllMingJiaModel modelWithDict:dict];
            
            [self.dataSourceArr addObject:model];
        }
        [self.collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
 
}
#pragma mark--collection的数据源代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HRAllMingJiaCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CELLID forIndexPath:indexPath];
    HRAllMingJiaModel *model=self.dataSourceArr[indexPath.row];
    cell.model=model;
    return cell;
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HRAllMingJiaModel *model=self.dataSourceArr[indexPath.row];
    HRJinRongViewController *next=[[HRJinRongViewController alloc]init];
    next.model=model;
    [self.navigationController pushViewController:next animated:YES];
    
}
@end
